<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class PickupPointDeliverApply extends Model
{
    use HasFactory, SoftDeletes;

    /**
     * 数据表名
     *
     * @var string
     */
    protected $table = 'ims_yz_pickup_point_deliver_apply';

    /**
     * 可批量赋值的属性
     *
     * @var array
     */
    protected $fillable = [
        'uid',
        'uniacid',
        'username',
        'password',
        'realname',
        'mobile',
        'status',
        'information',
        'salt',
        'pwd',
        'created_at',
        'updated_at'
    ];

    /**
     * 属性转换
     *
     * @var array
     */
    protected $casts = [
        'information' => 'array',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
    ];

    /**
     * 状态常量
     */
    const STATUS_PENDING = 0;    // 待审核
    const STATUS_APPROVED = 1;   // 审核通过
    const STATUS_REJECTED = -1;  // 审核不通过

    /**
     * 状态标签映射
     *
     * @var array
     */
    public static $statusLabels = [
        self::STATUS_PENDING => '待审核',
        self::STATUS_APPROVED => '审核通过',
        self::STATUS_REJECTED => '审核不通过',
    ];

    /**
     * 获取状态标签
     *
     * @return string
     */
    public function getStatusLabelAttribute()
    {
        return self::$statusLabels[$this->status] ?? '未知状态';
    }

    /**
     * 关联用户
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'uid');
    }

    /**
     * 作用域：待审核
     */
    public function scopePending($query)
    {
        return $query->where('status', self::STATUS_PENDING);
    }

    /**
     * 作用域：已审核通过
     */
    public function scopeApproved($query)
    {
        return $query->where('status', self::STATUS_APPROVED);
    }

    /**
     * 作用域：审核不通过
     */
    public function scopeRejected($query)
    {
        return $query->where('status', self::STATUS_REJECTED);
    }

    /**
     * 作用域：按创建时间排序
     */
    public function scopeOrderByCreated($query, $direction = 'desc')
    {
        return $query->orderBy('created_at', $direction);
    }
}
